capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B7
*/

local data "" ;
local output "" ;

foreach sample in all { ;

	use "prwora_bw264_crime_poly1allcov_iv_`sample'.dta", clear ;
	
	keep if 
		(
		 variables == "avg_1onssi_1997_2017" 	 | variables == "avg_2onssi_1997_2017" 	   | variables == "avg_3onssi_1997_2017" 	 | variables == "avg_4onssi_1997_2017" 	 |
		 variables == "avg_1kidearn_1997_2017"   | variables == "avg_2kidearn_1997_2017"   | variables == "avg_3kidearn_1997_2017"	 | variables == "avg_4kidearn_1997_2017" |
		 variables == "avg_1kidinc_1997_2017"  	 | variables == "avg_2kidinc_1997_2017"    | variables == "avg_3kidinc_1997_2017"	 | variables == "avg_4kidinc_1997_2017" |
		 variables == "avg_1kidinchh_1997_2017"  | variables == "avg_2kidinchh_1997_2017"  | variables == "avg_3kidinchh_1997_2017")	
		& sample == "age18unfav" ;
	forval i = 1(1)4 { ;
		replace variables = "onssi`i'"   if variables == "avg_`i'onssi_1997_2017" ;
		replace variables = "kearn`i'"   if variables == "avg_`i'kidearn_1997_2017" ;
		replace variables = "kdinc`i'"   if variables == "avg_`i'kidinc_1997_2017" ;
	} ;
	forval i = 1(1)3 { ;
		replace variables = "khinc`i'"   if variables == "avg_`i'kidinchh_1997_2017" ;
	} ;
	gen var = substr(variables,1,5) ;
	gen period = substr(variables,-1,1) ;
	destring period, replace ;

	rename n_age18unfav b_endo ;
	rename sen_age18unfav se_endo ;
	keep var period b_endo se_endo mean_rd ;

	gen lo95 = b_endo - 1.96*se_endo ;
	gen hi95 = b_endo + 1.96*se_endo ;
	
	gen 	sort = 2.3 	if  var == "onssi" & period == 1 ;
	replace	sort = 2.5 	if  var == "kdinc" & period == 1 ;
	replace	sort = 3 	if  var == "khinc" & period == 1 ;
	replace	sort = 3.3 	if  var == "kearn" & period == 1 ;

	replace	sort = 7.3 	if  var == "onssi" & period == 2 ;
	replace	sort = 7.5 	if  var == "kdinc" & period == 2 ;
	replace	sort = 8 	if  var == "khinc" & period == 2 ;
	replace	sort = 8.3 	if  var == "kearn" & period == 2 ;

	replace	sort = 12.3 	if var == "onssi" & period == 3 ;
	replace	sort = 12.5 	if var == "kdinc" & period == 3 ;
	replace	sort = 13 		if var == "khinc" & period == 3 ;
	replace	sort = 13.3 	if var == "kearn" & period == 3 ;

	replace	sort = 17.3 	if var == "onssi" & period == 4 ;
	replace	sort = 17.5 	if var == "kdinc" & period == 4 ;
	replace	sort = 18 		if var == "khinc" & period == 4 ;
	replace	sort = 18.3 	if var == "kearn" & period == 4 ;
	
	twoway 
		(rcap hi95 lo95 sort if var == "kearn", color(green*.5) lwidth(medthick) yaxis(1))
		(rcap hi95 lo95 sort if var == "kdinc", color(orange*.5) lwidth(medthick) yaxis(1))
		(rcap hi95 lo95 sort if var == "khinc", color(teal*.5) lwidth(medthick) yaxis(1))
		(rcap hi95 lo95 sort if var == "onssi", color(blue*.5) lwidth(medthick) yaxis(2))
		
		(scatter b_endo sort if var == "kearn", color(green) msize(large) msymbol(D)  yaxis(1))
		(scatter b_endo sort if var == "kdinc", color(orange) msize(large) msymbol(T)  yaxis(1))
		(scatter b_endo sort if var == "khinc", color(teal) msize(vlarge) msymbol(X) yaxis(1))
		(scatter b_endo sort if var == "onssi", color(blue)   msize(large) msymbol(O) yaxis(2))
		
		(pcarrowi -4900 3.5 	-4600 2.9, color(black))
		(pcarrowi -1500 1.5 -2000 2, color(black))
		(pcarrowi -100 4 	-500 3.5, color(black))
		(pcarrowi 2500 4 	2100 3.6, color(black))
		
		, 
		legend(off)
		xsc(r(0 20)) xlabel(2.5 "1997-01" 7.5 "2002-06" 12.5 "2007-12" 17.5 "2013-17") xtitle("")
					 ylab(-5000(2500)5000, axis(1)) ytitle("Earnings and income IV estimates", size(medsmall) axis(1))
					 ylab(-.5(.25).5, axis(2)) ytitle("On SSI IV estimate", size(medsmall) axis(2))
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		yline(0,lcolor(gray))
		ttext(-5000 4.4 "On SSI", size(vsmall))
		ttext(-1100 1.4 "18yo income" "(earnings + SSI)", size(vsmall))
		ttext(300 5 "18yo income +" "parent earnings", size(vsmall))
		ttext(2700 4 "18yo earnings", size(vsmall))
		;
		graph export "`output'/figB7.png", width(1000) height(750) replace ;
} ;


